﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Web_Oto.Bases;

namespace Web_Oto.Data
{
    public class NewsDal : dalObject
    {
        #region constructors

        public NewsDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {

        }

        public NewsDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }

        #endregion

        #region methods

        public IList<New> GetAllNews(string name = "", string intro = "")
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.News.Where(p => (p.Name.Contains(name) || string.IsNullOrEmpty(name)) &&
                    (p.Intro.Contains(intro) || string.IsNullOrEmpty(intro))).ToList();
            }
        }

        public IList<New> GetAllNews(int? currentPage, int itemPerPage, out int totalRecord)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var query = db.News.ToList();
                totalRecord = query.Count;
                return query.Skip((Convert.ToInt32(currentPage) - 1)*itemPerPage)
                    .Take(Convert.ToInt32(currentPage)*itemPerPage).ToList();
            }
        }

        public IList<New> GetTopNews(int showItem, bool? showHomePage)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                if (showHomePage == true)
                    return db.News.Where(n => n.IsHomePage == true).Take(showItem).ToList();
                return db.News.Where(n => n.IsHot == true).Take(showItem).ToList();
            }
        }

        public IList<New> GetAnotherNews(int newsId)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.News.Where(n => n.Id != newsId).OrderBy(n => n.Id).Take(5).ToList();
            }
        }

        public New GetNewsById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.News.FirstOrDefault(c => c.Id == id);
            }
        }

        public int Delete(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var news = db.News.FirstOrDefault(p => p.Id == id);
                    if (news != null)
                    {
                        db.News.DeleteOnSubmit(news);
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Update(New nNew)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var news = db.News.FirstOrDefault(p => p.Id == nNew.Id);
                    if (news != null)
                    {
                        news.Name = nNew.Name;
                        news.Intro = nNew.Intro;
                        news.TitleOfImage = nNew.TitleOfImage;
                        news.Description = nNew.Description;
                        news.IsHot = nNew.IsHot;
                        news.IsHomePage = nNew.IsHomePage;
                        news.Date = nNew.Date;
                        news.SEO_Title = nNew.SEO_Title;
                        news.SEO_Keyword = nNew.SEO_Keyword;
                        news.SEO_Description = nNew.SEO_Description;
                        if (!string.IsNullOrEmpty(nNew.Image))
                            news.Image = nNew.Image;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Insert(New nNew)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var news = new New
                                   {
                                       Name = nNew.Name,
                                       Intro = nNew.Intro,
                                       Description = nNew.Description,
                                       TitleOfImage = nNew.TitleOfImage,
                                       IsHot = nNew.IsHot,
                                       IsHomePage = nNew.IsHomePage,
                                       Date = nNew.Date,
                                       Image = nNew.Image,
                                       SEO_Title = nNew.SEO_Title,
                                       SEO_Keyword = nNew.SEO_Keyword,
                                       SEO_Description =nNew.SEO_Description
                                   };
                    db.News.InsertOnSubmit(news);
                    db.SubmitChanges();
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public string GetFilePathById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var news = db.News.FirstOrDefault(c => c.Id == id);
                if (news != null)
                    return news.Image;
            }

            return string.Empty;
        }

        #endregion
    }
}
